package cn.Controller;

import cn.Service.IndexService;
import cn.entity.Category;
import cn.entity.Product;
import cn.entity.Product_copy;
import cn.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private IndexService indexService;
    @GetMapping("/FindisHot")
    public String FindproductListByisHot(Model model){
        List<Product> products = indexService.FindproductListByisHot();
        List<Product> products1 = indexService.FindproductListByTime();
        List<Product> products2 = indexService.FindproductListBypbargain();
        List<Category> categories = indexService.FindCategoryAll();
        model.addAttribute("CategoriesList",categories);
        model.addAttribute("HOT_PRODUCT_LIST",products);
        model.addAttribute("NEW" +
                "_PRODUCT_LIST",products1);
        model.addAttribute("BARGAIN_PRODUCT_LIST",products2);
        return "index";
    }
    @GetMapping("/findProductByPid")
    public String findProductItemByPid(@RequestParam("pid") Integer pid, Model model, HttpServletRequest req){
        indexService.LittleData(pid);
        Product products = indexService.FindProductItemByPid(pid);
        String username =(String) req.getSession().getAttribute("username");
        if (username!=null){
        Product_copy product_copy = indexService.FindProduct_copyByPid(pid);
        if (product_copy==null){
            indexService.AddProduct(products);
        }else {
            indexService.DeleteProduct_copyByPid(pid);
            indexService.AddProduct(products);
        }
        }
        List<User> users = indexService.FindCommentByPid(pid);
        int i = indexService.FindCount(pid);
        model.addAttribute("count",i);
        model.addAttribute("Comment",users);
        model.addAttribute("PRODUCT",products);
        return "product_info";
    }


}
